<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<form class="layui-form" id="admin">

    <table class="layui-table" lay-even="" lay-skin="row" lay-size="sm">
        <colgroup>
            <col width="100">
            <col width="100">
            <col width="50">
        </colgroup>
        <thead>
        <tr>
            <th>价格类型</th>
            <th>默认价格</th>
            <th>购买份数</th>
        </tr>
        </thead>
        <tbody>

        {volist name="info" id="vo"}
        {if $vo.child_flag==0}
        <tr>
            <td>
                <input type="hidden" class="child_id" value="{$vo.price_type_id}">
                {$vo.type_name}
            </td>
            <td>
                <input type="number" class="price_value" value="{$vo.value}" style="width:100%;height:30px;">
            </td>
            <td>
                <input type="number" class="price_num" value="{$vo.num}" style="width:100%;height:30px;text-align: center">
            </td>
        </tr>
        {else}
        <tr>
            <td>{$vo.type_name}</td>
        </tr>
        {volist name="$vo.child" id="child"}
        <tr>
            <td>
                <input type="hidden" class="child_id" value="{$child.price_type_id}">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;————{$child.type_name}
            </td>
            <td>
                <input type="number" class="price_value" value="{$child.value}" style="width:100%;height:30px;">
            </td>
            <td>
                <input type="number" class="price_num" value="{$child.num}" style="width:100%;height:30px;text-align: center">
            </td>
        </tr>
        {/volist}
        {/if}
        {/volist}

        <tr style="font-weight:bold;">
            <td>
                合计：
            </td>
            <td colspan="3" id="count_price">
                {$count_price}
            </td>
        </tr>
        </tbody>
    </table>

    {notempty name="schedule_id"}
    <input type="hidden" name="schedule_id" id="schedule_id" value="{$schedule_id}">
    {/notempty}


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="admin">立即提交</button>
            <button id="reset1" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="__PUBLIC__/jquery/jquery.min.js"></script>
<script src="__PUBLIC__/layui/layui.js"></script>
<script>
    var message;
    layui.config({
        base: '__JS__/',
        version: '1.0.1'
    }).use(['app', 'message'], function() {
        var app = layui.app,
            $ = layui.jquery,
            layer = layui.layer;
        //将message设置为全局以便子页面调用
        message = layui.message;
        //主入口
        app.set({
            type: 'iframe'
        }).init();
    });
</script>
<script>

    $(".price_value").bind("input propertychange", function () {
        var count_price = count();
        $("#count_price").html(count_price);
    });
    $(".price_num").bind("input propertychange", function () {
        var count_price = count();
        $("#count_price").html(count_price);
    });

    //统计价格
    function count(){
        var count_price = 0;
        $(".price_value").each(function(){
            var value = $(this).val();
            var child_id = $(this).parent().parent().find(".child_id").val();
            var num = $(this).parent().parent().find(".price_num").val();
            var price = 0;
            if(value>0 && num>0){
                value = parseInt(value);
                num = parseInt(num);
                price = value*num;
                count_price += price;
            }
        });
        count_price = count_price.toFixed(2);
        return count_price;
    }


    layui.use(['layer', 'form'], function() {
        var layer = layui.layer,
            $ = layui.jquery,
            form = layui.form;
        $(window).on('load', function() {
            form.on('submit(admin)', function(data) {
                var schedule_id = $("#schedule_id").attr("value");
                var post_arr = [];
                var flag = 0;
                var count_price = 0;
                $(".price_value").each(function(){
                    var value = $(this).val();
                    var child_id = $(this).parent().parent().find(".child_id").val();
                    var num = $(this).parent().parent().find(".price_num").val();
                    var price = 0;
                    if(value>0 && num>0){
                        value = parseInt(value);
                        num = parseInt(num);
                        price = value*num;
                        count_price += price;
                        post_arr.push([child_id,value,num]);
                        flag++;
                    }
                });
                count_price = count_price.toFixed(2);

                var post_arr_json = JSON.stringify(post_arr);

                if(flag > 0){
                    $.ajax({
                        url:"{:url('admin/trip/price')}",
                        data:{'post_arr':post_arr_json,'count_price':count_price,'schedule_id':schedule_id},
                        type:'post',
                        async: false,
                        success:function(res) {
                            console.log(res);
                            layer.msg(res.msg);
                            if(res.code == 1) {
                                setTimeout(function(){
                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index); //再执行关闭
                                },1500);
                            }
                        }
                    })
                }else{
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                }
                return false;
            });
        });
    });
</script>
</body>
</html>
